import api from "./api";
import {
  ChatRequest,
  ChatResponse,
  ChatHistoryRecordRes,
} from "../types/index";

/**
 * 使用 AI 模型生成内容
 * @param data 聊天请求数据
 * @returns AI 生成的响应
 */
export const generateContent = async (
  data: ChatRequest
): Promise<ChatResponse> => {
  const response = await api.post<ChatResponse>("/ai/completion", data);
  return response.data as ChatResponse;
};

/**
 * 保存 AI 聊天历史记录
 * @param data 聊天历史数据
 * @returns 保存结果
 */
export const saveAIHistory = async (data: {
  id: number;
  history: string;
  uuid?: string;
}): Promise<{ message: string; data: string }> => {
  const response = await api.post<{ message: string; data: string }>(
    "/ai/saveAIHistory",
    data
  );
  return response.data as { message: string; data: string };
};

/**
 * 获取 AI 历史记录
 * @param page 页码
 * @param limit 每页记录数
 * @returns 分页的历史记录
 */
export const getAIHistory = async (
  page: number = 1,
  limit: number = 10
): Promise<{
  results: ChatHistoryRecordRes[];
  totalCount: number;
}> => {
  const response = await api.get<{
    results: ChatHistoryRecordRes[];
    totalCount: number;
  }>("/ai/getAIHistory", {
    params: { page, limit },
  });
  return response.data as {
    results: ChatHistoryRecordRes[];
    totalCount: number;
  };
};

/**
 * 删除 AI 历史记录
 * @param id 历史记录 ID
 * @returns 删除结果
 */
export const deleteAIHistory = async (
  id: number
): Promise<{ message: string }> => {
  const response = await api.delete<{ message: string }>(
    "/ai/deleteAIHistoryById",
    {
      params: { id },
    }
  );
  return response.data as { message: string };
};
